package com.ruoyi.qianhuankuan.controller;

import java.util.List;
import java.util.Objects;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.qiankuantongji.domain.chaxunRuselt;
import com.ruoyi.qiankuantongji.service.IQiankuantongjiService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.qianhuankuan.domain.Qianhuankuan;
import com.ruoyi.qianhuankuan.service.IQianhuankuanService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.ui.Model;
import org.springframework.web.context.request.SessionScope;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 欠还款表Controller
 * 
 * @author ruoyi
 * @date 2023-02-07
 */
@Controller
@RequestMapping("/qianhuankuan/qianhuankuanTest")
public class QianhuankuanController extends BaseController
{
    private String prefix = "qianhuankuan/qianhuankuanTest";

    @Autowired
    private IQianhuankuanService qianhuankuanService;

    @Autowired
    private IQiankuantongjiService qiankuantongjiService;

    @RequiresPermissions("qianhuankuan:qianhuankuanTest:view")
    @GetMapping()
    public String qianhuankuanTest()
    {
        return prefix + "/qianhuankuanTest";
    }

    /**
     * 查询欠还款表列表
     */
    @RequiresPermissions("qianhuankuan:qianhuankuanTest:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request, Qianhuankuan qianhuankuan)
    {
        startPage();
        String a = request.getSession().getAttribute("QuanXian").toString();
        String b=request.getSession().getAttribute("name4user").toString();
        if (a.equals("0") && (qianhuankuan == null || qianhuankuan.getCangku().equals(""))) {
            List<Qianhuankuan> namee2cangku = qianhuankuanService.selectAll2QianhuankuanList(b);

            return getDataTable(namee2cangku);
        }else {
            startPage();
            List<Qianhuankuan> list = qianhuankuanService.selectQianhuankuanList(qianhuankuan);
            return getDataTable(list);

        }
    }

    /**
     * 导出欠还款表列表
     */
    @RequiresPermissions("qianhuankuan:qianhuankuanTest:export")
    @Log(title = "欠还款表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Qianhuankuan qianhuankuan)
    {
        List<Qianhuankuan> list = qianhuankuanService.selectQianhuankuanList(qianhuankuan);
        ExcelUtil<Qianhuankuan> util = new ExcelUtil<Qianhuankuan>(Qianhuankuan.class);
        return util.exportExcel(list, "欠还款表数据");
    }

    @GetMapping("/addCheck")
    public String addCheck()
    {
        return prefix+"/historyKehu.html";
    }

    @PostMapping("/Check2Add")
    @ResponseBody
    public String Check2Add(HttpServletRequest request,String msg,Model model)
    {
        if (msg != null && !msg.equals("")) {
            if (msg.lastIndexOf("@") >= 1) {
                model.addAttribute("YuanYin","勾选了多项，此处仅可勾选一项");
                request.getSession().setAttribute("YuanYin","勾选了多项，此处仅可勾选一项");
                return "勾选了多项，此处仅可勾选一项";
            }else {
                String name = msg.replace("@","").replace(";","");
                request.getSession().setAttribute("CheckName",name);
                return "CheckName";
            }
        }
        model.addAttribute("YuanYin","勾选了多项，此处仅可勾选一项");
        request.getSession().setAttribute("YuanYin","没有勾选任何一项");
        return "没有勾选任何一项";
    }

    @PostMapping("/list2AllQian")
    @ResponseBody
    public TableDataInfo list2AllQian(HttpServletRequest request, chaxunRuselt chaxunRuselt)
    {
        startPage();
        String a = request.getSession().getAttribute("QuanXian").toString();
        String b=request.getSession().getAttribute("name4user").toString();
        if (a.equals("0") && (chaxunRuselt.getCangku() == null || chaxunRuselt.getCangku().equals(""))) {
            chaxunRuselt chaxunRuselt1 = new chaxunRuselt();
            chaxunRuselt1.setCangku(b);
            List<chaxunRuselt> namee2cangku = qiankuantongjiService.selectAllQiankuantongjiList2All(chaxunRuselt1);
            return getDataTable(namee2cangku);
        }else {
            startPage();
            List<chaxunRuselt> list = qiankuantongjiService.selectAllQiankuantongjiList2All(chaxunRuselt);
            return getDataTable(list);

        }
    }

    /**
     * 新增欠还款表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存欠还款表
     */
    @RequiresPermissions("qianhuankuan:qianhuankuanTest:add")
    @Log(title = "欠还款表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Qianhuankuan qianhuankuan)
    {
        return toAjax(qianhuankuanService.insertQianhuankuan(qianhuankuan));
    }

    /**
     * 修改欠还款表
     */
    @RequiresPermissions("qianhuankuan:qianhuankuanTest:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Qianhuankuan qianhuankuan = qianhuankuanService.selectQianhuankuanById(id);
        mmap.put("qianhuankuan", qianhuankuan);
        return prefix + "/edit";
    }

    /**
     * 修改保存欠还款表
     */
    @RequiresPermissions("qianhuankuan:qianhuankuanTest:edit")
    @Log(title = "欠还款表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Qianhuankuan qianhuankuan)
    {
        return toAjax(qianhuankuanService.updateQianhuankuan(qianhuankuan));
    }

    /**
     * 删除欠还款表
     */
    @RequiresPermissions("qianhuankuan:qianhuankuanTest:remove")
    @Log(title = "欠还款表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(qianhuankuanService.deleteQianhuankuanByIds(ids));
    }
}
